<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.6.6/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.6.6/themes/icon.css">
    <script type="text/javascript" src="../jquery-easyui-1.6.6/jquery.min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.6.6/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../js/jquery.serializeJSON-master/jquery.serializejson.min.js"></script>
    <title>用户</title>
    <style>
        .datagrid-view2 {
            overflow: scroll!important;
        }
    </style>
</head>

<body>
    <table id="dg" data-options="toolbar:toolbar"></table>
    <div id="dlg" class="easyui-dialog" title="Basic Dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px;height:180px;padding:10px">
        <form id="ff" class="easyui-form" method="post" data-options="novalidate:true">
            <input type="hidden" name="_id" />
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="name" style="width:100%" data-options="label:'Name:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="password" style="width:100%" data-options="label:'Password:',required:true">
            </div>
        </form>
        <div style="text-align:center;padding:5px 0">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">Clear</a>
        </div>
    </div>
</body>
<script>
    $('#dg').datagrid({
        url: `${parent.globalUrl}fl-users/list`,
        method: 'post',
        fit: true,
        pagination: true,
        columns: [
            [{
                field: 'ck',
                checkbox: true
            }, {
                field: 'name',
                title: 'name',
                width: 300
            }, {
                field: 'password',
                title: 'password',
                width: 100
            }, {
                field: '_id',
                title: 'id',
                width: 180,
                formatter: function(value, row, index) {
                    // console.log(row._id);
                    return `<a href = "javascript:editData('${row._id}')" > 修改 </a><a href = "javascript:deleData('${row._id}')">删除</a>`;
                }
            }]
        ],
        toolbar: [{
            text: 'Add',
            iconCls: 'icon-add',
            handler: function() {
                $('#dlg').dialog('open');
            }
        }, {
            text: '删除多个',
            iconCls: 'icon-cut',
            handler: function() {
                deleRows();
            }
        }]
    });

    function submitForm() {
        $('#ff').form('submit', {
            onSubmit: function() {
                //表单验证成功
                if ($(this).form('enableValidation').form('validate')) {
                    // console.log($('#ff').serializeJSON());
                    var formData = $('#ff').serializeJSON();
                    console.log(formData);
                    console.log($('#ff').serialize());
                    if (formData._id.length > 0) {
                        $.ajax({
                            url: `${parent.globalUrl}fl-users/data/` + formData._id,
                            type: 'put',
                            data: formData
                        }).then(res => {
                            console.log(res);
                            $('#dlg').dialog('close');
                            $('#dg').datagrid('reload');
                        })
                    } else {
                        delete formData._id;
                        $.ajax({
                            url: `${parent.globalUrl}fl-users/data`,
                            type: 'post',
                            data: formData
                        }).then(res => {
                            console.log(res);
                            $('#dlg').dialog('close');
                            $('#dg').datagrid('reload');
                        })
                    }
                }
                return false;
            }
        });
    }

    function clearForm() {
        $('#ff').form('clear');
    }

    function editData(id) {
        $('#ff').form('load', `${parent.globalUrl}fl-users/data/` + id);
        $('#dlg').dialog('open');
    }

    function deleData(id) {
        $.messager.confirm('提示', '你确认删除该数据么?', function(r) {
            if (r) {
                $.ajax({
                    url: `${parent.globalUrl}fl-users/data/` + id,
                    type: 'delete'
                }).then(res => {
                    $('#dg').datagrid('reload');
                })
            }
        });
    }

    function deleRows() {
        $.messager.confirm('提示', '你确认删除该数据么?', function(r) {
            if (r) {
                var selections = $('#dg').datagrid('getSelections');
                console.log(selections);
                if (selections.length > 0) {
                    var ids = [];
                    for (let i = 0; i < selections.length; i++) {
                        ids.push(selections[i]._id);
                    }
                    // console.log(ids);
                    $.ajax({
                        url: `${parent.globalUrl}fl-users/data/removes`,
                        type: 'post',
                        data: {
                            ids: ids.toString()
                        }
                    }).then(res => {
                        $('#dg').datagrid('reload');
                    })
                }
            }
        });
    }
</script>

</html>